<?
$ip=array('61.148.8.1','1.0.128.0','1.2.4.0','218.7.235.218');
$find=pack('N',ip2long('61.148.8.1'));
$index=unserialize(apc_fetch('area_index'));
$count=count($index);
$front=0;
while($front<$count-1){
	$i=(int)(($front+$count)/2);
	$start=strcmp($find,$index[$i]);
	if($start<0){
	 $count=$i;
	}else{
	   $end=strcmp($find,$index[$i+1]);
	   if($end>=0){
	   	$front=$i;
	   }else{
	   	$area=$index[$i];
		break;
	   }
	}
}
$ip=apc_fetch('area_'.$area);
$front=0;
$count=strlen($ip)/6;
$area=0;
while($front<$count-1){
	$i=(int)(($front+$count)/2);
	$start=strcmp($find,substr($ip,$i*6,4));
	if($start<0){
	  $count=$i;
	}else{
	    $end=strcmp($find,substr($ip,($i+1)*6,4));
		if($end>=0){
		  $front=$i;
		}else{
		  $area=unpack('C2',substr($ip,$i*6+4,2));
		  $area=$area[1]*256+$area[2];
		  break;
		}
	}
}
echo $area;
exit(0);
if(is_string($find)){
	$find=pack('N',ip2long($ip));
	$ip=file_get_contents('../../dat/ip/ip.cloud');
	$front=0;
	$count=strlen($ip)/6;
	$area=0;//查找到的地址ID
	$number=0;//循环多少次
	$start_time=microtime(true);
	while($front<$count-1){
	   $number++;
	   $i=(int)(($front+$count)/2);
	   $start=strcmp($find,substr($ip,$i*6,4));
	   if($start<0){
	   		$count=$i;
	   }else{
	   		$end=strcmp($find,substr($ip,($i+1)*6,4));
	   		if($end>=0){
	   			$front=$i;
	   		}else{
	   			$area=unpack('C2',substr($ip,$i*6+4,2));
				$area=$area[1]*256+$area[2];
		   		break;
	   		}
	   }
	}
	$end_time = microtime(true);
	echo 'time:'.(($end_time-$start_time)*1000).'ms ,while:'.$number.',area:'.$area.'<br />';
}else{
	foreach($ip as $value){
		$find=pack('N',ip2long($value));
		$ip=file_get_contents('../../dat/ip/ip.cloud');
		$front=0;
		$count=strlen($ip)/6;
		$area=0;//查找到的地址ID
		$number=0;//循环多少次
		$start_time=microtime(true);
		while($front<$count-1){
		   $number++;
		   $i=(int)(($front+$count)/2);
		   $start=strcmp($find,substr($ip,$i*6,4));
		   if($start<0){
		   		$count=$i;
		   }else{
		   		$end=strcmp($find,substr($ip,($i+1)*6,4));
		   		if($end>=0){
		   			$front=$i;
		   		}else{
		   			$area=unpack('C2',substr($ip,$i*6+4,2));
					$area=$area[1]*256+$area[2];
			   		break;
		   		}
		   }
		}
		$end_time = microtime(true);
		echo 'time:'.(($end_time-$start_time)*1000).'ms ,while:'.$number.',area:'.$area.'<br />';
	}
}

?>